
package com.AI;


import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;

import com.AI.R;
import com.google.android.maps.GeoPoint;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.WindowManager;
import android.widget.TextView;

public class CameraActivity extends Activity implements SurfaceHolder.Callback, SensorEventListener {

	private static final int TAKE_PHOTO_CODE = 1;
	private SurfaceView mSurfaceView;
	private SurfaceHolder mSurfaceHolder;
	private Camera mCamera;
	private boolean mPreviewRunning;
	private SensorManager mySensorManager;
	private SensorEventListener mySensorEventListener;
	private List<Sensor> mySensors;
	private CameraOverlayLayout cameraLayout;
	
	private int sensorOrientationIndex = 0;
	private int sensorAcceleroIndex = 0;
	
	public static Display display;
	
	public void onCreate(Bundle savedInstanceState)
	{
		
		super.onCreate(savedInstanceState );
		setContentView(R.layout.cameralayout);
		
		cameraLayout = (CameraOverlayLayout)findViewById(R.id.cameraOverlayLayout);
		cameraLayout.init();
		
		mSurfaceView = (SurfaceView) findViewById(R.id.surface_camera);
		mSurfaceHolder = mSurfaceView.getHolder();
		mSurfaceHolder.addCallback(this);
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		
		mySensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
		mySensors = mySensorManager.getSensorList(Sensor.TYPE_ALL);
		for (int i = 0; i<mySensors.size(); i++)
		{
			if (mySensors.get(i).getType() == Sensor.TYPE_ACCELEROMETER)
				sensorAcceleroIndex = i;
			if (mySensors.get(i).getType() == Sensor.TYPE_ORIENTATION)
				sensorAcceleroIndex = i;
							
		}
		
		display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
	}
	
	@Override
	protected void onResume()
	{
		super.onResume();
		Log.d("ADDING SENSORS:", sensorAcceleroIndex + " AND " + sensorOrientationIndex);
		mySensorManager.registerListener(this, mySensors.get(sensorAcceleroIndex), SensorManager.SENSOR_DELAY_GAME);
		mySensorManager.registerListener(this, mySensors.get(sensorOrientationIndex), SensorManager.SENSOR_DELAY_GAME);
		
	}
	
	@Override
	protected void onStop()
	{
		super.onStop();
		mySensorManager.unregisterListener(this, mySensors.get(sensorAcceleroIndex));
		mySensorManager.unregisterListener(this, mySensors.get(sensorOrientationIndex));		
	}
	
	@Override
	protected void onDestroy()
	{
		super.onDestroy();
		mySensorManager.unregisterListener(this, mySensors.get(sensorAcceleroIndex));
		mySensorManager.unregisterListener(this, mySensors.get(sensorOrientationIndex));
	}

	//@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		if (mPreviewRunning)
			mCamera.stopPreview();
		
		Camera.Parameters p = mCamera.getParameters();
		
		p.setPreviewSize(800, 480);
		mCamera.setParameters(p);
		try{
			mCamera.setPreviewDisplay(holder);
		} catch(IOException e){
			e.printStackTrace();
		}
		mCamera.startPreview();
		mPreviewRunning = true;
		
		cameraLayout.invalidate();
	}
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode == RESULT_OK)
		{
			switch(requestCode){
			case TAKE_PHOTO_CODE:
			}
		}
	}

	//@Override
	public void surfaceCreated(SurfaceHolder holder) {
		mCamera = Camera.open();
	}

	//@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		mCamera.stopPreview();
		mPreviewRunning = false;
		mCamera.release();
	}
	
	//@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
	}

	//@Override
	public void onSensorChanged(SensorEvent event) {
		synchronized (this) {
            if (event.sensor.getType() == Sensor.TYPE_ORIENTATION) {
            	UserModel.x_direction = event.values[0];            
            	UserModel.y_direction = event.values[1];
            	UserModel.z_direction = event.values[2];
            	
            }
            if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER) {
            	UserModel.x_acc = event.values[0];
            	UserModel.y_acc = event.values[1];
            	UserModel.z_acc = event.values[2] - 9.81;
            }
            ((TextView) findViewById(R.id.sensors)).setText("X Direction: " + UserModel.x_direction + 
    	    						"\nY Direction: " + UserModel.y_direction + "\nZ Direction: " + UserModel.z_direction + 
    	    						"\nX Acc: " + UserModel.x_acc + "\nY Acc: " + UserModel.y_acc + "\nZ Acc: " + UserModel.z_acc + "\nxAngle: " + (UserModel.ref_x_direction - UserModel.x_direction));
        	
            if (CameraOverlayLayout.tracking)
            	updateTracking();
		}
	}
	
	public void updateTracking()
	{
		// update usermodel etc. here
		double xDif = UserModel.ref_x_direction - UserModel.x_direction;
		double zDif = UserModel.ref_z_direction - UserModel.z_direction;
		int xPix = angleToPixel(xDif);
		int zPix = angleToPixel(zDif);
		
		UserModel.Xcoord = UserModel.screenWidthPix/2 + xPix;
		if (UserModel.y_direction > -90){
			UserModel.Ycoord = UserModel.screenHeightPix/2 - zPix;
		}
		else {
			UserModel.Ycoord = UserModel.screenHeightPix/2 + zPix;
		}
	}
	
	public int angleToPixel(double angle){
		int pixel = (int) (angle/45.0 * 800);
		return pixel;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER){
			cameraLayout.toggleTracking();
			return true;
		}
		return false;
	}
}
